package com.sdb.docter.controller;

import com.sdb.common.annotation.Log;
import com.sdb.common.core.controller.BaseController;
import com.sdb.common.core.domain.AjaxResult;
import com.sdb.common.core.page.TableDataInfo;
import com.sdb.common.enums.BusinessType;
import com.sdb.common.utils.poi.ExcelUtil;
import com.sdb.docter.domain.Schedule;
import com.sdb.docter.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/system/schedule")
public class ScheduleController extends BaseController {
    @Autowired
    private ScheduleService scheduleService;

    @PreAuthorize("@ss.hasPermi('system:schedule:list')")
    @GetMapping("/list")
    public TableDataInfo list(Schedule schedule) {
        startPage();
        List<Schedule> list = scheduleService.selectScheduleList(schedule);
        return getDataTable(list);
    }

    @Log(title = "Schedule", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:schedule:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Schedule schedule) {
        List<Schedule> list = scheduleService.selectScheduleList(schedule);
        ExcelUtil<Schedule> util = new ExcelUtil<>(Schedule.class);
        util.exportExcel(response, list, "Schedule Data");
    }

//    @PreAuthorize("@ss.hasPermi('system:schedule:query')")
//    @GetMapping(value = "/{scheduleId}")
//    public AjaxResult getInfo(@PathVariable("scheduleId") Long scheduleId) {
//        return success(scheduleService.selectScheduleById(scheduleId));
//    }

    @PreAuthorize("@ss.hasPermi('system:schedule:add')")
    @Log(title = "Schedule", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Schedule schedule) {
        return toAjax(scheduleService.insertSchedule(schedule));
    }

    @PreAuthorize("@ss.hasPermi('system:schedule:edit')")
    @Log(title = "Schedule", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Schedule schedule) {
        return toAjax(scheduleService.updateSchedule(schedule));
    }

    @PreAuthorize("@ss.hasPermi('system:schedule:remove')")
    @Log(title = "Schedule", businessType = BusinessType.DELETE)
    @DeleteMapping("/{scheduleIds}")
    public AjaxResult remove(@PathVariable Long[] scheduleIds) {
        return toAjax(scheduleService.deleteScheduleByIds(scheduleIds));
    }
}
